package test;

import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jekun.component.base.codegen.ConnectionCallback;
import cn.jekun.component.base.codegen.FreemarkerHelper;
import cn.jekun.component.core.bean.gen.ErpMerchandiseImage;

public class Main {

	public static void main(String[] args) throws Exception {
		//curd(false);
		list.add("0");
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		changeListPosition(2,3);
		
		changeListPosition(0,4);
	}

	public static List<String> list=new ArrayList<String>();
	
	public static void changeListPosition(int p1,int p2){
		
		String str=list.get(p1);
		
		list.set(p1, list.get(p2));
		
		list.set(p2, str);
		
		for(int i=0;i<list.size();i++){
			
			System.out.println(list.get(i));
		}
	}
	
	protected static void curd(final boolean delete) throws Exception{
		String projectRoot = null;
		URL classUrl = Main.class.getProtectionDomain().getCodeSource().getLocation();//.../source/sys/target/classes
		if("file".equals(classUrl.getProtocol())){
			projectRoot = new File(classUrl.getPath()).getParentFile().getParent();//.../source/sys
		}
		if(projectRoot == null){
			throw new NullPointerException("没有找到项目路径");
		}
		final String root = projectRoot;
		
		//curd(root);
		
		//createHeadHtml(root);
		
		createIndexHtml(root);
		
	}
	
	public static void curd(String root) throws Exception{
		
		if(root == null){
			
			throw new NullPointerException("没有找到项目路径");
		}
	
		List<ErpMerchandiseImage> photoList=new ArrayList<ErpMerchandiseImage>();
		
		ErpMerchandiseImage erpMerchandiseImage=new ErpMerchandiseImage();
		
		erpMerchandiseImage.setImageUrl("http://115.28.245.151/file/f/200/shop/5222000010/goods/0800004006-1.jpg");
		photoList.add(erpMerchandiseImage);
		photoList.add(erpMerchandiseImage);
		photoList.add(erpMerchandiseImage);
		
		Map context = new HashMap();
		
		context.put("merchandiseName", "aaaaaaaaaaaa");
		
		context.put("photoList",photoList);
		
		File outFile=null;
		
		String templatePath="/ftl/";
		
		String templateName=null;
		
		//html
		
		outFile=new File(root+"/src/main/java/model/merchandiseDetail.html");
		
		templateName="curd/merchandise_detail.ftl";
		
		FreemarkerHelper.produce(context,"UTF-8", outFile, templatePath, templateName,"UTF-8");
		
	}
	
	/**
	 * 生成头部静态页面
	 * @param root
	 * @throws Exception
	 */
	public static void createHeadHtml(String root) throws Exception{
		
		Map context = new HashMap();
		
		context.put("ctx", "/shop");
		
		context.put("fileUploadUrl","http://115.28.245.151/file/u");
		
		context.put("fileDownUrl","http://115.28.245.151/file/d");
		
		context.put("fileOpenUrl","http://115.28.245.151/file/f/200");
		
		context.put("userId", "001");
		
		context.put("userName", "test");
		
		File outFile=null;
		
		String templatePath="/ftl/";
		
		String templateName=null;
		
		//html
		
		outFile=new File(root+"/src/main/webapp/new/web/public/head.html");
		
		templateName="curd/head.ftl";
		
		FreemarkerHelper.produce(context,"UTF-8", outFile, templatePath, templateName,"UTF-8");
	}
	
	
	/**
	 * 生成首页静态页面
	 * @param root
	 * @throws Exception
	 */
	public static void createIndexHtml(String root) throws Exception{
		
		Map context = new HashMap();
		
		context.put("ctx", "/shop");
		
		context.put("fileUploadUrl","http://115.28.245.151/file/u");
		
		context.put("fileDownUrl","http://115.28.245.151/file/d");
		
		context.put("fileOpenUrl","http://115.28.245.151/file/f/200");
		
		context.put("headPath", "/src/main/webapp/new/web/public/head.html");
		
		File outFile=null;
		
		String templatePath="/ftl/";
		
		String templateName=null;
		
		//html
		
		outFile=new File(root+"/src/main/webapp/new/web/index.html");
		
		templateName="curd/index.ftl";
		
		FreemarkerHelper.produce(context,"UTF-8", outFile, templatePath, templateName,"UTF-8");
		
		//登录页
		outFile=new File(root+"/src/main/webapp/new/web/login.html");
		
		templateName="curd/login.ftl";
		
		FreemarkerHelper.produce(context,"UTF-8", outFile, templatePath, templateName,"UTF-8");
		
		
	}
}
